<%@ page language="java" import="java.util.*,com.mongodb.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
         response.setContentType("text/html;charset=utf-8");  
         request.setCharacterEncoding("utf-8");           
         String username=(String)request.getParameter("username");  //获取页面用户名
         String pwd=(String)request.getParameter("pwd");//获取注册页面密码1
         String pwd2=(String)request.getParameter("pwd2");//获取注册页面密码1
         String email=(String)request.getParameter("email");//获取注册页面邮箱
         System.out.println(username);
         if(!pwd.equals(pwd2)){  
        	 response.getWriter().write("密码不一致！");
        	 response.getWriter().close();
          }else{
        	  try{   
                  // 连接到 mongodb 服务
                  MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
                  DB db = mongoClient.getDB( "blog" );  //连接到数据库blog
                  DBCollection coll = db.getCollection("user");  //获取blog数据库中集合user
                 //验证注册用户名是否已经存在
                  DBObject repeat = new BasicDBObject();
                  repeat.put("username", username);
                  DBCursor records = coll.find(repeat);
                  if(records.length()!=0){
                	  System.out.println(records);
                	  response.getWriter().write("该用户名已经被注册！");
                 	  response.getWriter().close();
                  }else{
                	  DBObject user = new BasicDBObject();//定义一个Bson变量，用于存储注册的用户名和密码
                      user.put("username", username);
                      user.put("email", email);
                      user.put("pwd", pwd);            
                      coll.insert(user);    //向集合userInfo中插入注册用户信息                      
                      response.getWriter().write("true");
                      response.getWriter().close();
                  }
                  
               }catch(Exception e){
            	   response.getWriter().write("false");
            	   response.getWriter().close();
                  System.err.println( e.getClass().getName() + ": " + e.getMessage() );
              }    
          } 
     %>
</body>
</html>